//
//  LoginVC.m
//  SSC
//
//  Created by 红沙尘 on 17/6/7.
//  Copyright © 2017年 红沙尘. All rights reserved.
//

#import "LoginVC.h"
#import "SettingVC.h"
#import "KefuVC.h"
#import "AppDelegate.h"
#import "LoginManager.h"
#import "QRController.h"

@interface LoginVC ()
{
    __weak IBOutlet NSLayoutConstraint *_lineB;
    __weak IBOutlet NSLayoutConstraint *_lineA;
    __weak IBOutlet UIButton *_loginBtn;
    __weak IBOutlet UITextField *_nameField;
    __weak IBOutlet UITextField *_psdField;
    
    __weak IBOutlet UIButton *androidBtn;
    __weak IBOutlet UIButton *appBtn;
    LoginManager *_loginManager;
    
    
}

@end

@implementation LoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTitle:@"赢龍娱乐"];
    
    [self setupUI];
}

#pragma mark - IBAction

- (IBAction)gotoAndroid {
    [self.navigationController pushViewController:[[QRController alloc] initWithQRType:QRTypeAndroid]
                                         animated:YES];
}

- (IBAction)gotoApple {
    [self.navigationController pushViewController:[[QRController alloc] initWithQRType:QRTypeApple]
                                         animated:YES];
}

- (IBAction)closeKeyboard {
    [_nameField resignFirstResponder];
    [_psdField resignFirstResponder];
}

- (IBAction)forgetPasssword {
    KefuVC *kf = [[KefuVC alloc] init];
    kf.type = KefuTypeMima;
    
    [self.navigationController pushViewController:kf animated:YES];
}

- (IBAction)login {
    
    [LoadingView show];
    
    [SSCRequest loginWithAccount:_nameField.text
                        password:_psdField.text
                         success:^(NSDictionary *responseObject)
     {
         NSDictionary *dict = responseObject[@"data"];
         
         Userinfo *userinfo =  [Userinfo standardUserinfo];
        
         BOOL isLogin = [userinfo saveUserinfoWithUid:dict[@"id"]
                                             username:dict[@"username"]
                                             password:_psdField.text
                                                token:dict[@"token"]];
         
         if (isLogin) {
             AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
             [app gotoMain];
         }
         
         [LoadingView diss];
        
    } fail:^(NSError *error) {
        [LoadingView diss];
    }];
}

#pragma mark - private

- (void)setupData {
    _loginManager = [[LoginManager alloc] init];
}

- (void)setupUI {
    
    _lineA.constant = LineHeight;
    _lineB.constant = LineHeight;
    _loginBtn.layer.cornerRadius  = 22;
    _loginBtn.layer.masksToBounds = YES;
    
    appBtn.layer.cornerRadius = 5;
    appBtn.layer.masksToBounds = YES;
    appBtn.layer.borderColor = COLOR_FF5253.CGColor;
    appBtn.layer.borderWidth = LineHeight;
    androidBtn.layer.cornerRadius = 5;
    androidBtn.layer.masksToBounds = YES;
    androidBtn.layer.borderColor = COLOR_FF5253.CGColor;
    androidBtn.layer.borderWidth = LineHeight;
    
    _nameField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _psdField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    UIButton *leftBtn = [self leftButtonWithImageNamed:@"login_setting" action:@selector(gotoSetting)];
    self.navigationItem.leftBarButtonItem = CUSTOMVIEW(leftBtn);
    
    UIButton *rightBtn = [self rightButtonWithImageNamed:@"login_kefu" action:@selector(gotoKefu)];
    self.navigationItem.rightBarButtonItem = CUSTOMVIEW(rightBtn);
}

- (void)gotoSetting {
    [self.navigationController pushViewController:[[SettingVC alloc] init] animated:YES];
}

- (void)gotoKefu {
    
    KefuVC *kf = [[KefuVC alloc] init];
    kf.type = KefuTypeKefu;
    
    [self.navigationController pushViewController:kf animated:YES];
    
    //如果是拨打客服电话用下面代码
//    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@""
//                                                                             message:@"拨打客服电话"
//                                                                      preferredStyle:UIAlertControllerStyleAlert];
//    
//    [alertController addAction:[UIAlertAction actionWithTitle:@"取消"
//                                                        style:UIAlertActionStyleCancel
//                                                      handler:^(UIAlertAction * _Nonnull action) {
//                                                          
//                                                      }]];
//    
//    [alertController addAction:[UIAlertAction actionWithTitle:@"拨打"
//                                                        style:UIAlertActionStyleDefault
//                                                      handler:^(UIAlertAction * _Nonnull action)
//    {
//        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", KefuPhone]];
//        [[UIApplication sharedApplication] openURL:url];
//        
//    }]];
//    
//    [self presentViewController:alertController animated:YES completion:nil];
}



@end
